package com.mqtt.ctwingmqtt.service.Impl;


import com.mqtt.ctwingmqtt.domain.dto.UserDTO;
import com.mqtt.ctwingmqtt.domain.entity.User;
import com.mqtt.ctwingmqtt.mapper.UserMapper;
import com.mqtt.ctwingmqtt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询角色信息
     * @param username
     * @return
     */
    @Override
    public String getRole(String username) {
        String rolename = userMapper.getRole(username);
        return rolename;
    }

    /**
     * 查询密码
     * @param username
     * @return
     */
    @Override
    public String getPassword(String username) {
        String psw=userMapper.getPassword(username);
        return psw;
    }

    /**
     * 注册校验
     * @param user
     * @return
     */
    @Override
    public Boolean insect(UserDTO user) {
        //判断用户名是否存在
        User existUser = userMapper.selectByName(user.getUsername());
        if(existUser!=null){
            return false;
        }
        User user1 = new User();
        user1.setUsername(user.getUsername());
        user1.setPassword(user.getPassword());
        user1.setEmail(user.getEmail());
        user1.setMobile(user.getMobile());
        user1.setNickname(user.getNickname());
        //设置创建时间为当前时间
        user1.setCreateTime(new Timestamp(System.currentTimeMillis()).toLocalDateTime());


        return userMapper.insert(user1)>0;
    }

    /**
     * 根据角色名获取角色id
     * @param role
     * @return
     */
    @Override
    public Integer getRoleIdByRoleName(String role) {
        return userMapper.getRoleIdByRoleName(role);
    }

    /**
     * 根据角色id获取权限
     * @param roleId
     * @return
     */
    @Override
    public List<String> getPermissionsByRoleId(Integer roleId) {
        return userMapper.getPermissionsByRoleId(roleId);
    }


}

